package com.ruoyi.service.jpjs.bi_dynamiccost.impl;

import com.ruoyi.mapper.jpjs.bi_dynamiccost.DynamicCostMapper;
import com.ruoyi.service.jpjs.bi_dynamiccost.DynamicCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DynamicCostServiceImpl implements DynamicCostService {

    @Autowired
    private DynamicCostMapper dynamicCostMapper;

    @Override
    public List<Map<String, Object>> selectDynamicCostList(Map<String, Object> map) {
        return dynamicCostMapper.selectDynamicCostList(map);
    }

    @Override
    public Map<String, Object> selectDynamicCost(String id) {
        return dynamicCostMapper.selectDynamicCost(id);
    }

    @Override
    public int addDynamicCost(Map<String, Object> map) {
        map=getDefaultMap(map);
        dynamicCostMapper.addDynamicCost(map);
        return dynamicCostMapper.addDynamicSituation(map);
    }

    @Override
    public int editDynamicCost(Map<String, Object> map) {
        map=getDefaultMap(map);
        if(map.get("sid")!=null && "".equals(map.get("sid"))){
            dynamicCostMapper.editDynamicSituation(map);
        }else{
            dynamicCostMapper.addDynamicSituation(map);
        }
        return dynamicCostMapper.editDynamicCost(map);
    }

    @Override
    public int deleteDynamicCost(Map<String, Object> map) {
        if(map.get("sid")!=null && "".equals(map.get("sid"))){
            dynamicCostMapper.deleteDynamicSituation(map.get("sid").toString());
        }
        return dynamicCostMapper.deleteDynamicCost(map.get("id").toString());
    }

    private Map<String, Object> getDefaultMap(Map<String, Object> map) {
        if ("".equals(map.get("planIncome"))) {
            map.put("planIncome", 0);
        }
        if ("".equals(map.get("planCost"))) {
            map.put("planCost", 0);
        }
        if ("".equals(map.get("planProfitRate"))) {
            map.put("planProfitRate", 0);
        }
        if ("".equals(map.get("IncomeChange"))) {
            map.put("IncomeChange", 0);
        }
        if ("".equals(map.get("dynamicIncome"))) {
            map.put("dynamicIncome", 0);
        }
        if ("".equals(map.get("costChange"))) {
            map.put("costChange", 0);
        }
        if ("".equals(map.get("dynamicCost"))) {
            map.put("dynamicCost", 0);
        }
        if ("".equals(map.get("dynamicProfitRate"))) {
            map.put("dynamicProfitRate", 0);
        }
        return map;
    }
}
